package com.trkj.usercenter.feedback;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.trkj.base.BaseDataRefreshFragment;
import com.trkj.base.Constants;
import com.trkj.base.User;
import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;

import java.text.MessageFormat;

/**
 * 反馈意见
 * Created by LiZanhong on 2015/06/25.
 */
public class FeedbackFragment extends BaseDataRefreshFragment{

    public int getUid() {
        User user = UserService.getInstance(this.getActivity()).parseUser();
        return user != null ? user.userId : 0;
    }

    @Override
    public String getUrl() {
        String url = MessageFormat.format(Constants.ApiUrl.FEEDBACK, getUid());
        Log.d("URL", url);
        return url;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //先定义Adapter
        if(getAdapter() == null) {
            setAdapter(new FeedbackAdapter());
        }
        View root = super.onCreateView(inflater, container, savedInstanceState);
        return root;
    }

    class FeedbackAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return getData().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;

            if(convertView == null){
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(getActivity()).inflate(
                        R.layout.activity_feedback_item, null);

                viewHolder.feedback = (TextView)convertView.findViewById(
                        R.id.feedback);

                viewHolder.hadReply = (LinearLayout) convertView.findViewById(R.id.had_reply);
                viewHolder.hadReplyFeedbackDate = (TextView)convertView.findViewById(
                        R.id.had_reply_feedback_date);
                viewHolder.hadReplyContent = (TextView)convertView.findViewById(
                        R.id.had_reply_content);

                viewHolder.noReply = (LinearLayout) convertView.findViewById(R.id.no_reply);
                viewHolder.nowNoReply = (TextView)convertView.findViewById(
                        R.id.now_no_reply);
                viewHolder.feedbackDate = (TextView)convertView.findViewById(
                        R.id.feedback_date);

                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }

            JSONArray data = getData();
            JSONObject jsonObject = data.getJSONObject(position);

            viewHolder.feedback.setText(jsonObject.getString("content"));

            String reply = data.getJSONObject(position).getString("reply");
            if(TextUtils.isEmpty(reply)){
                //无回复
                viewHolder.noReply.setVisibility(View.VISIBLE);
                viewHolder.hadReply.setVisibility(View.GONE);

                viewHolder.nowNoReply.setText("暂无回复");
                viewHolder.feedbackDate.setText("反馈时间：" +
                        jsonObject.getString("create_time").substring(0, 10));
            }else{
                //有回复
                viewHolder.noReply.setVisibility(View.GONE);
                viewHolder.hadReply.setVisibility(View.VISIBLE);

                viewHolder.hadReplyContent.setText(jsonObject.getString("reply"));
                viewHolder.hadReplyFeedbackDate.setText("反馈时间：" +
                        jsonObject.getString("create_time")
                                .substring(0, 10));
            }

            return convertView;
        }

        class ViewHolder{
            TextView feedback;

            LinearLayout hadReply;
            TextView hadReplyFeedbackDate;
            TextView hadReplyContent;

            LinearLayout noReply;
            TextView nowNoReply;
            TextView feedbackDate;
        }
    }

}
